package com.ljf.tijian.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljf.tijian.ApiResponse;
import com.ljf.tijian.mapper.UsersMapper;
import com.ljf.tijian.pojo.Sms.SMS;
import com.ljf.tijian.pojo.Users;
import com.ljf.tijian.service.UserService;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import jakarta.servlet.http.HttpServletResponse;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 处理用户模块请求的控制器
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    UsersMapper usersMapper;

    /**
     * 处理登录请求
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ApiResponse login(@RequestBody Users user, HttpServletResponse response) throws JsonProcessingException {

        String userId = user.getUserId();
        String password = user.getPassword();
        System.out.println("login接口接收的参数为 userId: "+userId+",password: "+password);
//        return userService.login(user);
        ApiResponse apiResponse = userService.login(user);
        Users users = (Users)apiResponse.getData();
        if(apiResponse.getStatus() == 200){
            Map<String,Object> map = new HashMap<>();
            map.put("currentUser",new ObjectMapper().writeValueAsString(users));

            JwtBuilder builder = Jwts.builder()
                    .setId(UUID.randomUUID().toString())
                    .setIssuedAt(new Date())
                    .setClaims(map).setSubject(user.getUserId())
                    .signWith(SignatureAlgorithm.HS256,"123456")
                    .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60))
                    ;

            String key = builder.compact();
            response.setHeader("token",key);
            System.out.println("返回JWT令牌");

            return apiResponse;
        }

        return apiResponse;
    }

    @RequestMapping("/register")
    public ApiResponse register(@RequestBody Users user){
        return userService.register(user);
    }

    @PostMapping("/smsget")
    public ApiResponse smsget(@RequestBody Map<String,String> map){
        return userService.sendSms(map);
    }

    @PostMapping("/smslogin")
    public ApiResponse smslogin(@RequestBody SMS sms,HttpServletResponse response)throws JsonProcessingException{

        String userId = sms.getPhone();
        String Sms = sms.getSms();
        System.out.println("login接口接收的参数为 userId: "+userId+",Sms: "+Sms);

        ApiResponse apiResponse = userService.smsLogin(sms);
        Users users = usersMapper.selectByPrimaryKey(userId);
//        Users users = (Users)apiResponse.getData();
        if(apiResponse.getStatus() == 200){

            Map<String,Object> map = new HashMap<>();
            map.put("currentUser",new ObjectMapper().writeValueAsString(users));

            JwtBuilder builder = Jwts.builder()
                    .setId(UUID.randomUUID().toString())
                    .setIssuedAt(new Date())
                    .setClaims(map).setSubject(sms.getPhone())
                    .signWith(SignatureAlgorithm.HS256,"123456")
                    .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 20))
                    ;

            String key = builder.compact();
            response.setHeader("token",key);
            System.out.println("返回JWT令牌");

            return apiResponse;
        }

        return apiResponse;
    }
}
